package com.wn.icloud.provider.uac.controller;

import com.wn.icloud.auth.client.annotation.IgnoreUserToken;
import com.wn.icloud.common.msg.ObjectRestResponse;
import com.wn.icloud.common.utils.UploadUtil;
import com.wn.icloud.provider.uac.service.EnterpriseInfoService;
import com.wn.icloud.provider.uac.service.dto.EnterpriseInfoDTO;
import com.wn.icloud.rest.BaseController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * 企业信息表(EnterpriseInfo)表控制层
 *
 * @author makejava
 * @since 2020-04-24 22:23:02
 */
@RestController
@RequestMapping("enterpriseInfo")
public class EnterpriseInfoController {
    /**
     * 服务对象
     */
    @Resource
    private EnterpriseInfoService enterpriseInfoService;

    /**
     * 展示企业码
     *
     * @param id 企业主键id
     * @return 单条数据
     */
    @GetMapping("queryCompanyCode")
    public ObjectRestResponse queryCompanyCode(String id) {
        return this.enterpriseInfoService.queryById(id);
    }

    /**
     * 企业登记并生码
     * @param enterpriseInfoDTO 企业信息
     * @return
     */
    @PostMapping("companyRegister")
    public ObjectRestResponse companyRegister(
            @RequestBody EnterpriseInfoDTO enterpriseInfoDTO,
            @RequestParam(name = "userId") String userId) {

        return enterpriseInfoService.companyRegister(enterpriseInfoDTO, userId);
    }
    /**
     * 上传文件
     * @param  request 请求
     * @return
     */
    @IgnoreUserToken
    @PostMapping("fileUpload")
    public ObjectRestResponse fileUpload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        String fileName = multipartFile.getOriginalFilename();
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
        }catch (IOException e){
            throw new RuntimeException("读取文件时出错，请重试", e);
        }
        UploadUtil.uploadFtpFile(inputStream,fileName);
        String path = "http://114.116.121.16:9876/"+fileName;
        return ObjectRestResponse.success(path);
    }

    /**
     * 获取企业数字身份
     * @param companyName 企业名称
     * @param creditCode 信用代码
     * @param legalPersonName 法人姓名
     * @param idCard 法人身份证号
     * @return
     */
    @GetMapping("companyPortraits")
    public ObjectRestResponse companyPortraits(
            String companyName,
            String creditCode,
            String legalPersonName,
            String idCard) {
        return enterpriseInfoService.companyPortraits(companyName, creditCode, legalPersonName, idCard);
    }



}